<template>
	<view>
		<view class="app-1"><image src="https://gnzxwylz.xlygrcw.com/upload/GNZX/login/1.png" mode=""></image></view>
		<view class="btn">
			<view class=""><button type="default" open-type="getUserInfo" @getuserinfo="wxGetUserInfo">微信登录</button></view>
		</view>
		<view class="btn">
			<view class=""><button type="default" @tap="onClose">取消</button></view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {};
	},
	onLoad() {},
	methods: {
		onClose(){
			uni.switchTab({
				url:'../tabBar/home/index'
			})
		},
		wxGetUserInfo(e) {
			let that = this;
			uni.login({
				provider: 'weixin',
				success: function(res) {
					that.request({
						url: that.interfaces.getOpenId,
						data: {
							code: res.code
						},
						success: user => {
							console.log(user);
							uni.setStorageSync('session_key', user.session_key);
							const data = {};
							data.openid = user.openid;
							data.headimgurl = e.detail.userInfo.avatarUrl;
							data.sex = e.detail.userInfo.gender;
							data.nickname = e.detail.userInfo.nickName;
							data.name = e.detail.userInfo.nickName;
							that.onSetRegistration(data);
						},
						fail(e) {
							uni.showToast({
								title: '登录失败'
							});
						}
					});
					console.log(e);
					console.log(res);
				}
			});
		},
		onSetRegistration(data) {
			this.request({
				url: this.interfaces.getUserInfo,
				method: 'POST',
				data: data,
				title: '登录中...',
				success: res => {
					console.log(res.data);
					uni.setStorageSync('gnzx_users', res.data);
					uni.switchTab({
						url: '../tabBar/home/index'
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.app-1 {
	display: flex;
	height: 500upx;
	align-items: center;
	justify-content: center;
	padding: 150upx 0 50upx;
	image {
		width: 340upx;
		height: 260upx;
	}
}
.btn {
	padding: 20upx 40upx;
	button {
		background-color: #07c160;
		height: 88upx;
		font-size: 36upx;
		color: #fff;
		text-align: center;
		line-height: 88upx;
		border-radius: 88upx;
	}
}
</style>
